package com.test.reference;

import lombok.extern.slf4j.Slf4j;

import java.lang.ref.SoftReference;

/**
 * 软引用Demo
 * 1.软引用在内存够用时不回收，不够用的时候回收
 * 2.demo正常时候运行
 * 3.在配置-Xms5m -Xmx5m -XX:+PrintGCDetails下运行
 *
 * @author wangjie
 * @version V1.0
 * @date 2020/1/4
 */
public class SoftReferencrDemo {
    public static void main(String[] args) {

        Object obj1 = new Object();
        SoftReference<Object> softReference = new SoftReference<>(obj1);

        System.out.println(obj1);
        System.out.println(softReference.get());

        obj1 = null;
        System.gc();

        try {

            byte[] b = new byte[10 * 1024 * 1024];
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println(obj1);
            System.out.println(softReference.get());
        }

    }
}
